<?php

namespace app\api\command;
use app\common\model\Packages;
use app\common\model\User;
use app\common\model\UserCommission;
use app\common\model\UserPackagesConfig;
use fast\Random;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class Fenyong extends Command
{
    protected function configure()
    {
        $this->setName('fenyong')->setHelp('分佣')->setDescription('订单分佣');

    }

    protected function execute(Input $input, Output $output)
    {
        //is_agency
        exit();
        $data = db('order')->where(['order_status'=>4,'is_agency'=>1,'jiesuan_time'=>0])->select();
        if($data){
            foreach ($data as $k=>$v){
                    $userData = findAncestors($v['user_id']);
                    $PackagesInfo     = Packages::where(['id'=>$v['goods_id']])->find();
                    if($PackagesInfo){
                        if(!$userData){
                            //顶级代理下单
                            dingji($v['user_id'],$v['id'],$v['goods_id'],$PackagesInfo['rebate']);
                        }else{
                            array_unshift($userData,$v['user_id']);
                            $money = moneydigui($v['user_id'],$v['goods_id']);
                            if($money){
                                $this->digui($v['user_id'], $money,  $v['goods_id'],  $v['id']);
                            }else{
                                //顶级
                                dingji($v['user_id'],$v['id'],$v['goods_id'],$PackagesInfo['rebate']);
                            }
                        }
                    }
                    db('order')->where(['id'=>$v['id']])->update(['jiesuan_time'=>time()]);
            }
        }
        echo "操作成功";
    }

    public function digui($user_id,$money,$goods_id,$order_id){
        $userModel    = new \app\common\model\User();
        $Commission   = new UserCommission();
        $config       = new UserPackagesConfig();
        $PackagesInfo = Packages::where(['id'=>$goods_id])->find();
        if($PackagesInfo){
            if($money > 0){
                $userInfo   = $userModel->where(['id'=>$user_id])->find();
                $userConfig = $config->where(['user_id'=>$user_id,'packages_id'=>$PackagesInfo['id']])->find();
                if($userConfig){
                    if($userConfig['day'] == 1){
                        $inser['settlement_time']  = strtotime(date("Y-m-d 18:00:00"));
                    }else if($userConfig['day'] == 6){
                        $kai_time = time()+86400*30;
                        $kai_time = date("Y-m-d 18:00:00",$kai_time);
                        $inser['settlement_time']  = strtotime($kai_time);
                    }else{
                        $kai_time = time()+86400*$userConfig['day'];
                        $kai_time = date("Y-m-d 18:00:00",$kai_time);
                        $inser['settlement_time']  = strtotime($kai_time);
                    }
                    $inser['user_id']         = $user_id;
                    $inser['money']           = $money;
                    $inser['order_id']        = $order_id;
                    $inser['packages_id']     = $goods_id;
                    $Commission->create($inser);
                }else{
                    if($userInfo['parent_id'] == 0){
                        dingji($user_id,$order_id,$goods_id,$money,false);
                    }
                }
                if($userInfo['parent_id'] > 0){
                    $configInfo = $config->where(['user_id'=>$userInfo['parent_id'],'packages_id'=>$PackagesInfo['id']])->find();
                    if($configInfo){
                        if($userConfig){
                            $money = $configInfo['money'] - $userConfig['money'];
                        }

                    }else{
                        $money = $PackagesInfo['rebate'] - $userConfig['money'];
                    }
                    return $this->digui($userInfo['parent_id'], $money,$goods_id,$order_id);
                }
            }
        }
    }

}
